<?php

function save_session($ulogin, $upass)
{
  $inOneDay = 24 * 60 + time();
  setcookie('shoplogin', $ulogin, $inOneDay);
  setcookie('shoppass', $upass, $inOneDay);
}

function slay_session()
{
  setcookie('shoplogin', "", time() - 3600);
  setcookie('shoppass', "", time() - 3600);
}
function remove_from_cart($item_id)
{
  $inOneDay = 24 * 60 + time();
  if (isset($_COOKIE['shopcart']))
    {
      $item_list = $_COOKIE['shopcart'];
      $match = '/' . $item_id . '+-[0-9]+;' . '/';
      $item_list = preg_replace($match, '', $item_list);
      setcookie('shopcart', $item_list, $inOneDay);
    }
  return 0;
}

function get_cart_item()
{
  $ret_item = array();
  $count = 0;
  if (isset($_COOKIE['shopcart']))
    $item_list = $_COOKIE['shopcart'];
  $id_quan = explode(';',$item_list);
  foreach ($id_quan as $item_quan)
    {
      if ($count == count($id_quan) - 1)
	break;
      $item = explode('-', $item_quan);      
      $ret_item[] = array(
			'id' => $item[0],
			'quan' => $item[1]
			);
      $count++;
    }
  return $ret_item;
}
  
    
function add_to_cart ($item_id, $hmn)
{
  /* TODO: browser react slow */
  if (isset($_COOKIE['shopcart']))
    $item_list = $_COOKIE['shopcart'];
  else
      $item_list = "";
  
  $item_list .= $item_id . "-" . $hmn . ";";
  $inOneDay = 24 * 60 + time();
  setcookie('shopcart', $item_list, $inOneDay);
}
?>